//* 关键字+驾车路线规划，编辑页使用 已弃用 */
$(document).ready(function(){
    getAmaps(); //初始化
});

//地图查询
function getAmaps() {
    //驾车策略
    var _policy = $("#select_policy").find("option:selected").val();
    if (_policy === undefined) {
        _policy = "AMap.DrivingPolicy.LEAST_TIME";
    }

    //基本地图加载
    var map = new AMap.Map("amapContainer", {
        resizeEnable: true,
        center: [116.397428, 39.90923],//地图中心点
        zoom: 13 //地图显示的缩放级别
    });

    //加载地址工具
    map.plugin(["AMap.ToolBar"], function () {
        map.addControl(new AMap.ToolBar());
    });
    if (location.href.indexOf('&guide=1') !== -1) {
        map.setStatus({scrollWheel: false})
    }

    //构造路线导航类
    var driving = new AMap.Driving({
        map: map,
        panel: "amapPanel",
        policy: _policy
    });

    //起&途经&终点
    //单独写一个方法获取页面输入数据
    var n_points = createPoints();

    if (n_points.length > 1) {
        // 根据起终点名称规划驾车导航路线
        driving.search(n_points, function (status, result) {
            // result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult

            var _info = $("#returnInfo");   //获取返回信息DOM
            _info.children("li").remove();  //清空历史信息

            if (status === 'complete') {

                $("input[name='origin']").val(result.origin);
                $("input[name='destination']").val(result.destination);

                _info.append('<li>起点：' + result.start.pname + ' - ' + result.start.cityname + ' - ' + result.start.adname + ' - ' + result.start.name + '</li>');
                for (k = 0; k < result.waypoints.length; k++) {
                    _info.append('<li>途经点' + (k + 1) + '：' + result.waypoints[k].pname + ' - ' + result.waypoints[k].cityname + ' - ' + result.waypoints[k].adname + ' - ' + result.waypoints[k].name + '</li>');
                }
                _info.append('<li>终点：' + result.end.pname + ' - ' + result.end.cityname + ' - ' + result.end.adname + ' - ' + result.end.name + '</li>');
                _info.append('<li>&nbsp;</li>');

                var r = result.routes;
                for (i = 0; i < r.length; i++) {
                    _info.append('<li>全程：' + miToMili(r[i].distance) + '公里</li>');
                    _info.append('<li>预计：' + seToMili(r[i].time) + '分钟</li>');
                    _info.append('<li>路费：' + r[i].tolls + '元</li>');
                    _info.append('<li>&nbsp;</li>');

                    $("input[name='distance_km']").val(miToMili(r[i].distance));
                    $("input[name='distance_mi']").val(r[i].distance);
                    $("input[name='time']").val(seToMili(r[i].time));
                    $("input[name='tolls']").val(r[i].tolls);

                    // 设置行车策略
                    var _getvalue = getPolicy(_policy);
                    $("input[name='policy_id']").val(_getvalue.split(",")[0]);
                    $("input[name='policy_name']").val(_getvalue.split(",")[1]);

                    /* *** 不展示具体行程信息 ***
                    var s = r[i].steps;
                    _info.append('<li>行程开始</li>');
                    for (j = 0; j < s.length; j++) {
                        _info.append('<li>' + (j + 1) + '、[' + s[j].orientation + ']' + s[j].instruction + '（' + s[j].time + '秒）</li>');
                    }
                    _info.append('<li>行程结束</li>');
                    ******/
                }
            } else {
                _info.append('<li>获取驾车数据失败：' + result.info + '</li>');
            }
        });
    }
}

//保存地图信息
function saveAmaps() {

    //取值
    var origin = $("input[name='origin']").val();
    var destination = $("input[name='destination']").val();
    var distance_km = $("input[name='distance_km']").val();
    var distance_mi = $("input[name='distance_mi']").val();
    var time = $("input[name='time']").val();
    var tolls = $("input[name='tolls']").val();

    var start_city = $("input[name='start_city']").val();
    var start_point = $("input[name='start_point']").val();

    var steps_city1 = $("input[name='steps_city1']").val();
    var steps_point1 = $("input[name='steps_point1']").val();
    var steps_city2 = $("input[name='steps_city2']").val();
    var steps_point2 = $("input[name='steps_point2']").val();
    var steps_city3 = $("input[name='steps_city3']").val();
    var steps_point3 = $("input[name='steps_point3']").val();
    var steps_city4 = $("input[name='steps_city4']").val();
    var steps_point4 = $("input[name='steps_point4']").val();
    var steps_city5 = $("input[name='steps_city5']").val();
    var steps_point5 = $("input[name='steps_point5']").val();
    var steps_city6 = $("input[name='steps_city6']").val();
    var steps_point6 = $("input[name='steps_point6']").val();
    var steps_city7 = $("input[name='steps_city7']").val();
    var steps_point7 = $("input[name='steps_point7']").val();
    var steps_city8 = $("input[name='steps_city8']").val();
    var steps_point8 = $("input[name='steps_point8']").val();
    var steps_city9 = $("input[name='steps_city9']").val();
    var steps_point9 = $("input[name='steps_point9']").val();
    var steps_city10 = $("input[name='steps_city10']").val();
    var steps_point10 = $("input[name='steps_point10']").val();
    var steps_city11 = $("input[name='steps_city11']").val();
    var steps_point11 = $("input[name='steps_point11']").val();
    var steps_city12 = $("input[name='steps_city12']").val();
    var steps_point12 = $("input[name='steps_point12']").val();

    var end_city = $("input[name='end_city']").val();
    var end_point = $("input[name='end_point']").val();

    var policy_id = $("input[name='policy_id']").val();
    var policy_name = $("input[name='policy_name']").val();

    var id = $("input[name='id']").val();
    var bookId = $("input[name='bookId']").val();
    var editTravel = $("#editTravel").val();

    var add_cycle = $("input[name='add_cycle']").val();

    $.ajax({
        url: "/transport/schedule/saveEditTravel",
        type: "POST",
        data: {
            id: id,
            bookId: bookId,
            startCity: start_city,
            startPoint: start_point,
            endCity: end_city,
            endPoint: end_point,
            passCity1: steps_city1,
            passPoint1: steps_point1,
            passCity2: steps_city2,
            passPoint2: steps_point2,
            passCity3: steps_city3,
            passPoint3: steps_point3,
            passCity4: steps_city4,
            passPoint4: steps_point4,
            passCity5: steps_city5,
            passPoint5: steps_point5,
            passCity6: steps_city6,
            passPoint6: steps_point6,
            passCity7: steps_city7,
            passPoint7: steps_point7,
            passCity8: steps_city8,
            passPoint8: steps_point8,
            passCity9: steps_city9,
            passPoint9: steps_point9,
            passCity10: steps_city10,
            passPoint10: steps_point10,
            passCity11: steps_city11,
            passPoint11: steps_point11,
            passCity12: steps_city12,
            passPoint12: steps_point12,
            startDestination: origin,
            endDestination: destination,
            policy: policy_id,
            policyName: policy_name,
            runTolls: tolls,
            runTime: time,
            distance: distance_mi,
            distanceKm: distance_km,
            editTravel: editTravel,
            addCycle: add_cycle
        },
        dataType: 'json',
        async: false,
        error: function (request) {
            //error
        },
        success: function (data) {
            showInfoAlert(data.returnMessage, gotoRedirectUrl);
        }
    });
}

//简单粗暴的方法组装一下
function createPoints() {
    var n_points = [];

    //起点
    var start_city = $("input[name='start_city']").val();
    var start_point = $("input[name='start_point']").val();
    if (start_city !== "" && start_city !== undefined && start_point !== "" && start_point !== undefined) {
        n_points.push(
            {
                keyword: start_point, city: start_city
            }
        )
    }

    //途径：城市
    var _citys = [];
    $("input[name^='steps_city']").each(function () {
        _citys.push($(this).val());
    });

    //途经：具体点
    var _points = [];
    $("input[name^='steps_point']").each(function () {
        _points.push($(this).val());
    });

    //组装城市和具体途经点
    for (i = 0; i < _citys.length; i++) {
        if (_citys[i] !== "" && _points[i] !== "") {
            n_points.push(
                {
                    keyword: _points[i], city: _citys[i]
                }
            )
        }
    }

    //终点
    var end_city = $("input[name='end_city']").val();
    var end_point = $("input[name='end_point']").val();
    if (end_city !== "" && end_city !== undefined && end_point !== "" && end_point !== undefined) {
        n_points.push(
            {
                keyword: end_point, city: end_city
            }
        )
    }

    return n_points;
}

// "米" 转化为 "公里"
function miToMili(mi) {
    if (mi === "" || mi === null){
        return 0;
    }

    var num = mi / 1000;
    num = num.toFixed(2);
    return num;
}

// "秒" 转化为 "分钟"
function seToMili(se) {
    if (se === "" || se === null){
        return 0;
    }

    var num = se / 60;
    num = num.toFixed(0);
    return num;
}

// 行车策略
function getPolicy(name) {
    if (name === "AMap.DrivingPolicy.LEAST_TIME") {
        return "1,最快捷模式";
    }
    if (name === "AMap.DrivingPolicy.LEAST_FEE") {
        return "2,最经济模式";
    }
    if (name === "AMap.DrivingPolicy.LEAST_DISTANCE") {
        return "3,最短距离模式";
    }
    if (name === "AMap.DrivingPolicy.REAL_TRAFFIC") {
        return "4,考虑实时路况";
    }

    return "1,最快捷模式";
}